package com.enjoyor.soa.traffic.restt.tpcms.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.enjoyor.soa.traffic.frame.security.bean.dto.UserDto;

import com.alibaba.fastjson.JSONObject;
import com.enjoyor.soa.traffic.api.dto.tpcms.InfoStopRoadDto;
import com.enjoyor.soa.traffic.api.dto.tpcms.StopRoadDto;
import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.restt.tpcms.dubbo.impl.StopRoadConsumeService;
import com.enjoyor.soa.traffic.util.helper.GeomtryHelper;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Auth
@RestController
@RequestMapping({ "/StopRoad" })
@Api("禁行线路操作Controller")
@CrossOrigin
public class StopRoadController {

    @Autowired
    private StopRoadConsumeService stopRoadConsumeService;

    /**
     * 查询线路分页信息
     * 
     * @param stopRoadId
     * @param stopRoadName
     * @param applyZoneIdList
     * @param roadTypeId
     * @param isUse
     * @param page
     * @param request
     * @return
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @GetMapping(value = { "/getStopRoadPage" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation(value = "查询禁行线路分页信息", notes = "查询数据库中禁行线路基本信息记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stopRoadId", value = "stopRoadId", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "stopRoadName", value = "stopRoadName", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "roadTypeId", value = "roadTypeId", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "isUse", value = "isUse", paramType = "query", dataType = "string") })
    public ResultPojo getStopRoadPage(
            @RequestParam(defaultValue = "", required = false, value = "stopRoadId") String stopRoadId,
            @RequestParam(defaultValue = "", required = false, value = "stopRoadName") String stopRoadName,
            @RequestParam(required = false, defaultValue = "", value = "applyZoneIdList[]") String[] applyZoneIdList,
            @RequestParam(defaultValue = "", required = false, value = "roadTypeId") String roadTypeId,
            @RequestParam(defaultValue = "", required = false, value = "isUse") String isUse, Page page,
            HttpServletRequest request) {
        /* Arrays.asList返回的列表底层为数组，尺寸不可改变 */
        List<String> asList = Arrays.asList(applyZoneIdList);
        String applyZoneIdListString = JSONObject.toJSONString(asList);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("stopRoadId", stopRoadId);
        map.put("stopRoadName", stopRoadName);
        map.put("applyZoneIdList", asList);
        map.put("roadTypeId", roadTypeId);
        map.put("isUse", isUse);
        page.setMapBean(map);
        Page roadPage = (Page) stopRoadConsumeService.getStopRoadPage(page).getResultList();
        roadPage.getMapBean().put("applyZoneIdList", applyZoneIdListString);
        //String result = ResultHelper.getResultJson(roadPage).toJSONString();
        return new ResultPojo(roadPage);
    }

    /**
     * 查询线路列表
     * 
     * @param stopRoadId
     * @param stopRoadName
     * @param applyZoneIdList
     * @param roadTypeId
     * @param isUse
     * @param request
     * @return
     */
    @SuppressWarnings("unchecked")
    @GetMapping(value = { "/getStopRoadList" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation(value = "查询禁行线列表信息", notes = "查询数据库中禁行线路列表记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stopRoadId", value = "stopRoadId", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "stopRoadName", value = "stopRoadName", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "roadTypeId", value = "roadTypeId", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "isUse", value = "isUse", paramType = "query", dataType = "string") })
    public ResultPojo getStopRoadList(
            @RequestParam(defaultValue = "", required = false, value = "stopRoadId") String stopRoadId,
            @RequestParam(defaultValue = "", required = false, value = "stopRoadName") String stopRoadName,
            @RequestParam(required = false, defaultValue = "", value = "applyZoneIdList[]") String[] applyZoneIdList,
            @RequestParam(defaultValue = "", required = false, value = "roadTypeId") String roadTypeId,
            @RequestParam(defaultValue = "", required = false, value = "isUse") String isUse,
            HttpServletRequest request) {
        // Arrays.asList返回的列表底层为数组，尺寸不可改变
        List<String> asList = Arrays.asList(applyZoneIdList);
        List<StopRoadDto> stopRoadList = (List<StopRoadDto>) stopRoadConsumeService
                .getStopRoadList(stopRoadId, stopRoadName, asList, roadTypeId, isUse).getResultList();
        // 处理dto的地理信息数据，以字符串的形式传入前端
        for (StopRoadDto stopRoadDto : stopRoadList) {
            if (stopRoadDto.getRoadGeo() != null) {
                String geom2JsonString = GeomtryHelper.getGeom2JsonString(stopRoadDto.getRoadGeo());
                stopRoadDto.setRoadGeoString(geom2JsonString);
            }
        }
        //String jsonString = ResultHelper.getResultJson(stopRoadList).toJSONString();
        return new ResultPojo(stopRoadList);
    }

    /**
     * 新增线路数据
     * 
     * @param infoStopRoad
     * @param request
     * @return
     */
    @PostMapping(value = { "/insertStopRoad" }, consumes = { "application/json; charset=UTF-8" }, produces = {
            "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("新增禁行线路")
    public ResultPojo InsertStopRoad(@RequestBody InfoStopRoadDto infoStopRoad, HttpServletRequest request) {
        return stopRoadConsumeService.insertStopRoad(infoStopRoad,
                ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }

    /**
     * 修改线路数据
     * 
     * @param infoStopRoad
     * @param request
     * @return
     */
    @PutMapping(value = { "/editStopRoad" }, consumes = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("更新线路数据")
    public ResultPojo editStopRoad(@RequestBody InfoStopRoadDto infoStopRoad, HttpServletRequest request) {
        return stopRoadConsumeService.editStopRoad(infoStopRoad,
                ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }

    /**
     * 删除线路数据
     * 
     * @param stopRoadId
     * @param request
     * @return
     */
    @DeleteMapping(value = { "/deleteStopRoad" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("删除线路数据")
    @ApiImplicitParam(name = "stopRoadId", value = "stopRoadId", paramType = "query", required = true, dataType = "String")
    public ResultPojo deleteStopRoad(@RequestParam(value = "stopRoadId", required = true) String stopRoadId,
            HttpServletRequest request) {
        return stopRoadConsumeService.deleteStopRoad(stopRoadId,
                ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }

    /**
     * 绘制地图
     * 
     * @param geom
     * @param stopRoadId
     * @param request
     * @return
     */
    @PutMapping(value = { "/drawingRoadShape" }, consumes = { "application/x-www-form-urlencoded" })
    @ResponseBody
    @ApiOperation("绘制地图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stopRoadId", value = "stopRoadId", paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "geom", value = "geom", paramType = "query", dataType = "string") })
    public ResultPojo drawingRoadShape(@RequestParam(required = true, value = "geom") String geom,
            @RequestParam(required = true, value = "stopRoadId") String stopRoadId, HttpServletRequest request) {
        InfoStopRoadDto infoStopRoad = new InfoStopRoadDto();
        infoStopRoad.setStopRoadId(stopRoadId);
        return stopRoadConsumeService.drawingRoadShape(infoStopRoad, geom,
                ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }

}
